package com.cssl.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class ConsumerConfig {
    /**
     * 延迟交换机(x-delayed-type、x-delayed-message固定写法)
     */
    @Bean("delayExchange")
    public CustomExchange delayExchange(){
        Map<String,Object> args = new HashMap<>();
        args.put("x-delayed-type","direct");
        return new CustomExchange("delayExchange","x-delayed-message",true,true,args);
    }

    @Bean("delayQueue")
    public Queue delayQueue(){
        return new Queue("delayQueue",true,false,false);
    }

    @Bean
    public Binding bindingDelayExchangeQueue(CustomExchange delayExchange, @Qualifier("delayQueue") Queue delayQueue){
        return BindingBuilder.bind(delayQueue).to(delayExchange)
                .with("delayRoutkey").noargs();
    }

    /**
     * 直连
     * @return
     */
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange("direct",true,true);
    }

    @Bean
    public Queue q12(){
        Map<String,Object> args = new HashMap<>();
        // args参数设置队列过期时间10秒，10秒后所有消息清空或进入死信队列
        args.put("x-message-ttl",10000);
        args.put("x-dead-letter-exchange","deadExchange");
        args.put("x-dead-letter-routing-key","deadRouting");
        return new Queue("q12",true,false,false,args);
    }
    @Bean
    public Binding bindingExchangeQueue12(@Qualifier("directExchange")DirectExchange directExchange, @Qualifier("q12") Queue queue12) {
        return BindingBuilder.bind(queue12)
                .to(directExchange).with("rk");
    }

    /*//死信交换机
    @Bean
    public DirectExchange deadExchange(){
        return new DirectExchange("deadExchange",true,false);
    }

    //死信队列
    @Bean
    public Queue deadQueue(){
        Map<String,Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange","deadExchange");
        args.put("x-dead-letter-routing-key","deadRouting");
        return new Queue("deadQueue",true,false,false,args);
    }

    @Bean
    public Binding bindingExchangeQueue13() {
        return BindingBuilder.bind(deadQueue())
                .to(deadExchange()).with("deadRouting");
    }*/
}
